<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\GoldExchangeModel;
use App\Model\GoldRecordModel;
use App\Model\PkPeopleModel;
use App\Model\PkRecordModel;
use App\Model\SignModel;
use App\Model\WebUserModel;
use Illuminate\Http\Request;
use DB;

class StatisticsController extends Controller{

    public function index()
    {
        return view('Admin/statistics/index');
    }

    public function search(Request $request)
    {
        $start_date = $request->input('startDate');
        $end_date   = $request->input('endDate');

        if(!isset($start_date) || !isset($end_date)){
            $start_date = date('Y-m-d',strtotime('-10 day'));
            $end_date   = date('Y-m-d');
        }

        $res_data = array(
            'total' => 0,
            'rows' => array()
        );
        if($start_date && $end_date){
            if($end_date < $start_date){
                return response()->json($res_data);
            }
        }

        if(strtotime($start_date) == strtotime($end_date)){
            $index = 1;
        }else{
            $index = ((strtotime($end_date) - strtotime($start_date)) / 86400) +1;
        }


        $start_time = $start_date. ' 00:00:00';
        $end_time = $start_date. ' 23:59:59';
        $rows_array = [];
        for ($i = 0; $i < $index; $i++){

            //签到人数
            $sign_count = SignModel::where('dtModifyTime','>=',$start_time)
                ->where('dtModifyTime','<=',$end_time)
                ->count();
            //PK人数
            $pk_num = PkPeopleModel::where('dtCreateTime','>=',$start_time)
                ->where('dtCreateTime','<=',$end_time)
                ->groupBy('iUserID')
                ->count();

            //PK局数
            $pk_count = PkRecordModel::where('dtEndTime','>=',$start_time)
                ->where('dtEndTime','<=',$end_time)
                ->where('iStatus',2)
                ->count();

            //净消耗豆数
            $gold_record_count = GoldRecordModel::where('dtModifyTime','>=',$start_time)
                ->where('dtModifyTime','<=',$end_time)
                ->where('iGoldBeanConsume','<=',0)
                ->select(DB::raw('sum(iGoldBeanConsume) as gold'))
                ->get()
                ->toArray();
            //抢三玩法
            $pk_count_three = PkRecordModel::where('dtEndTime','>=',$start_time)
                ->where('dtEndTime','<=',$end_time)
                ->where(['iStatus'=>2,'sPkType'=>1])
                ->count();

            //新增用户数
            $user_count = WebUserModel::where('iRegistTime','>=',$start_time)
                ->where('iRegistTime','<=',$end_time)
                ->groupBy('iSeqId')->count();

            $rows_array[] = [
                'id'                =>  $i+1,
                'date_time'         =>  date('Y-m-d',strtotime($start_time)),
                'sign_count'        => isset($sign_count) ? $sign_count : 0,
                'pk_count'          => isset($pk_count) ? $pk_count : 0,
                'pk_num'            => isset($pk_num) ? $pk_num : 0,
                'gold_record_count' => isset($gold_record_count) ? $gold_record_count[0]['gold'] : 0,
                'pk_count_three'    => isset($pk_count_three) ? $pk_count_three : 0,
                'pk_count_five'     => $pk_count - $pk_count_three < 0 ? 0 : $pk_count - $pk_count_three,
                'user_count'        => isset($user_count) ? $user_count : 0,
            ];
            $start_time = date('Y-m-d H:i:s',strtotime($start_time)+86400);
            $end_time = date('Y-m-d H:i:s',strtotime($end_time) +86400);

        }
        $page = ($request->input('page') - 1)*$request->input('limit');

        $rows_array = array_slice($rows_array,$page,$request->input('limit'));
        //返回数组
        $res_data = array(
            'total' => $index,
            'rows' => $rows_array
        );

        return response()->json($res_data);
    }

    public function details(Request $request,$id){

        $pageIndex = $request->input('page',1);
        $limit     = $request->input('limit',10);
        if(!isset($id) || empty($id)){
            return response()->json(array('code' => 404,'message' => '系统错误，请联系管理员'));
        }
        $user_data = WebUserModel::where('iSeqId',$id)->first();

        if(!$user_data){
            return response()->json(array('code' => 404,'message' => '该用户不存在，请联系管理员'));
        }

        $gold_record = GoldRecordModel::where('iUserId',$id)
            ->paginate(10)
            ->toArray();


        $res_data = [];

        if($gold_record['data']){

            foreach ($gold_record['data'] as $list){
                $iConsumeType = '';
                switch ($list['iConsumeType']){
                    case 1:
                        $iConsumeType = 'PK成功';
                        break;
                    case 2:
                        $iConsumeType = 'PK失败';
                        break;
                    case 3:
                        $iConsumeType = '兑换';
                        break;
                    case 4:
                        $iConsumeType = '签到';
                        break;
                }

                $res_data[] = [
                    'id'           => $list['iSeqId'],
                    'dtModifyTime' => $list['dtModifyTime'],
                    'type'         => $iConsumeType,
                    'iGoldBeanConsume' => $list['iGoldBeanConsume']
                ];
            }
        }
        $page_link = '';

        if($gold_record['total'] > 0){
            $page_link = page($pageIndex,$gold_record['total'],$limit,'javascript:void(0)','Admin.page');
        }

        $data = [
            'search_id' => $id,
            'total'     => $gold_record['total'],
            'link'      => (string)$page_link,
            'rows'      => $res_data
        ];

        return  view('Admin/user/detail', $data);
    }

    public function details_search(Request $request){

        $id = $request->input('id');

        if(!isset($id) || empty($id)){
            return response()->json(array('code' => 404,'message' => '系统错误，请联系管理员'));
        }
        $user_data = WebUserModel::where('iSeqId',$id)->first();

        if(!$user_data){
            return response()->json(array('code' => 404,'message' => '该用户不存在，请联系管理员'));
        }

        $gold_record = GoldRecordModel::where('iUserId',$id)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $res_data = array(
            'total' => $gold_record['total'],
            'rows' => array()
        );

        if($gold_record){

            foreach ($gold_record['data'] as $list){
                $iConsumeType = '';
                switch ($list['iConsumeType']){
                    case 1:
                        $iConsumeType = 'PK成功';
                        break;
                    case 2:
                        $iConsumeType = 'PK失败';
                        break;
                    case 3:
                        $iConsumeType = '兑换';
                        break;
                    case 4:
                        $iConsumeType = '签到';
                        break;
                }

                $res_data['rows'][] = [
                    'id'           => $list['iSeqId'],
                    'dtModifyTime' => $list['dtModifyTime'],
                    'type'         => $iConsumeType,
                    'iGoldBeanConsume' => $list['iGoldBeanConsume']
                ];
            }
        }

        return response()->json($res_data);
    }

    public function change($id){

        return view('Admin/user/change',['data'=>$id]);
    }

    public function change_search(Request $request){

        $id = $request->input('id');
        if(!isset($id) || empty($id)){
            return response()->json(array('code' => 404,'message' => '系统错误，请联系管理员'));
        }
        $where='';
        if($id=='all'){
            $where=[];
        }else{
            $user_data = WebUserModel::where('iSeqId',$id)->first();

            if(!$user_data){
                return response()->json(array('code' => 404,'message' => '改用户不存在，请联系管理员'));
            }
            $where=['iUserId'=>$id];
        }


        $change = GoldExchangeModel::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $res_data = array(
            'total' => $change['total'],
            'rows' => array()
        );

        if($change){
            foreach ($change['data'] as $list){
                $user=WebUserModel::where('iSeqId',$list['iUserId'])->first();
                $res_data['rows'][] = [
                    'id'           => $list['iSeqId'],
                    'dtModifyTime' => $list['dtModifyTime'],
                    'user_name'    =>  empty($user->sUserName) ?   '' : urldecode($user->sUserName),
                    'iExGold'      => $list['iExGold']
                ];
            }
        }

        return response()->json($res_data);

    }
}